Implement custom functions
We will implement the detail in doTransformRow and doProcessRow, he details of the functions are changed by your requirement. In this example, we will use these function to handle two requirements:
- If Org Id Prefix is provided, automatically add the prefix to the organisation ID
- If Default Org Type is selected, automatically add all organisations in the file to the selected organisation type
Input data

Upload action

Upload mapping

Result

Implements
You can see in the input file and mapping, we will see two issues:
- It does not contains the prefix in the input file
- The organisation type does not existed in the input file. Even if the field is existed in the file, it does not mapped in the mapping
To resolve these issues, we will implement function handler for transformRowFn and processRownFn:
- transformRowFn is handled by doTransformRow
- processRownFn is handled by doProcessRow
function doTransformRow(row, params, rowContext, importContext){
var orgIdPrefix = params.orgIdPrefix;
if ( formatter.isNotEmpty(orgIdPrefix) ) {
var orgId = formatter.safeGet(row, 0);
if ( formatter.isNotEmpty(orgId) ) {
orgId = orgIdPrefix + orgId;
}
row.set(0, formatter.cleanString(orgId)); //Modify the first element
}
var defaultOrgType = params.defaultOrgType;
if ( formatter.isNotEmpty(defaultOrgType) ) {
row.add(defaultOrgType); //Add the org type to the end of row data (Index = 3)
}
return row;
}
Each row in the input file will be transform to an array list (the first parametter in the function), and each cell in the row will be an element of the array list.
- Add the prefix: The Company ID is mapped to Org ID, and it is first element of the array list, so we will modify the first element of the array list.
- Add the organistion type to the row data: The transform function cannot process the organisation data, it only prepare the data for the process function. As you can see, the row have not contains the value for organisation type, so we will add the org type to the end of the row data (In this case, it is the fourth element). Then we will process this element in the doProcessRow
function doProcessRow(row, params, rowContext, importContext){
var fieldMappings = rowContext.fieldMappings;
if (fieldMappings && formatter.isNotEmpty(fieldMappings)) {
var typeMap = formatter.newArrayList();
typeMap.add(3); //The index of the org type in row data
fieldMappings.put('orgTypeName', typeMap);
}
}
In the doProcessRow, we will modify the current mapping and add the org type (orgTypeName) to the mapping. The field will look for the org type value in the row data by its index